package com.google.code.javascribd.connection;
//package com.google.code.javascribd.connection;
//
//import java.io.File;
//import java.io.UnsupportedEncodingException;
//import java.lang.reflect.UndeclaredThrowableException;
//import java.net.Proxy;
//import java.net.URLEncoder;
//import java.util.Map;
//
//import org.apache.commons.httpclient.HttpClient;
//import org.apache.commons.httpclient.HttpStatus;
//import org.apache.commons.httpclient.methods.PostMethod;
//import org.apache.commons.httpclient.methods.multipart.FilePart;
//import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
//import org.apache.commons.httpclient.methods.multipart.Part;
//import org.apache.commons.httpclient.params.HttpMethodParams;
//
//import com.google.code.javascribd.method.ScribdMethod;
//import com.google.code.javascribd.type.ScribdType;
//import com.google.code.javascribd.type.interpretor.ScribdTypeInterpretor;
//
//public class ApacheHTTPConnection implements HTTPConnection {
//
//	private Proxy proxy = null;
//	/* TODO, Apr 30, 2009, meyke - required */
//	private String url = null;
//
//	public void setProxy(Proxy proxy) {
//		this.proxy = proxy;
//
//	}
//
//	public void setUrl(String url) {
//		this.url = url;
//	}
//
//	private Proxy getProxy() {
//		return proxy;
//	}
//
//	private String getUrl() {
//		return url;
//	}
//
//	public <T extends ScribdType> T getRequest(ScribdMethod<T> method, ScribdTypeInterpretor interpretor) {
//		/* TODO, May 1, 2009, meyke - */
//		throw new UnsupportedOperationException();
//	}
//
//	public <T extends ScribdType> T postRequest(ScribdMethod<T> method, ScribdTypeInterpretor interpretor) {
//		String targetURL = this.getUrl() + this.getEncodedUrlParameters(method.getMethodName(), method.getParameters());
//      /* TODO, May 1, 2009, meyke - get file from method*/
//		File targetFile = new File("name");
//		PostMethod filePost = new PostMethod(targetURL);
//
//		filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
//
//		try {
//			System.out.println("Uploading " + targetFile.getName() + " to " + targetURL);
//			Part[] parts = { new FilePart("file", targetFile) };
//			filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
//			HttpClient client = new HttpClient();
//			client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
//			int status = client.executeMethod(filePost);
//			if (status == HttpStatus.SC_OK) {
//				return interpretor.interpret(filePost.getResponseBodyAsStream(), method.getResultTypeClass());
//			} else {
//				System.out.println("Upload failed, response=" + HttpStatus.getStatusText(status));
//				throw new IllegalStateException(HttpStatus.getStatusText(status));
//			}
//		} catch (Exception ex) {
//			System.out.println("ERROR: " + ex.getClass().getName() + " " + ex.getMessage());
//			ex.printStackTrace();
//			throw new UndeclaredThrowableException(ex);
//		} finally {
//			filePost.releaseConnection();
//		}
//	}
//
//	private String getEncodedUrlParameters(String methodName, Map<String, String> parameters) {
//		StringBuffer sb = new StringBuffer();
//		sb.append("?method=");
//		sb.append(methodName);
//
//		for (String key : parameters.keySet()) {
//			sb.append("&");
//			sb.append(key);
//			sb.append("=");
//			String value = parameters.get(key);
//			String encodedValue;
//			try {
//				encodedValue = URLEncoder.encode(value.toString(), "UTF-8");
//			} catch (UnsupportedEncodingException e) {
//				throw new IllegalStateException("UTF-8 is a unknown encoding.", e);
//			}
//			sb.append(encodedValue);
//		}
//		return sb.toString();
//	}
//
//}
